package com.chexiehui.jc.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.chexiehui.jc.utils.NoDoubleClickListener;
import com.chexiehui.jc.utils.NoDoubleClickUtils;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.List;


/**
 *
 * Created by SongJun on 2017/8/23.
 */

public abstract class CommVLayoutAdapter<T> extends DelegateAdapter.Adapter<ViewHolder> {
    //上下文
    private Context mContext;
    //布局文件资源ID
    private int mResLayout;
    //数据源
    private List<T> mDatas;
    //布局管理器
    private LayoutHelper mLayoutHelper;
    //回调监听
    private OnItemClickListener<T> mCLickListener;
    private OnItemLongClickListener<T> mLongListener;

    /**
     * <br/> 方法名称:VBaseAdapter
     * <br/> 方法详述:构造函数
     * <br/> 参数:<同上申明>
     */
    public CommVLayoutAdapter(Context context, List<T> listData, int itemLayout, LayoutHelper layoutHelper) {
        if (context == null) {
            throw new RuntimeException("context is null,please check your params !");
        }
        if (listData == null) {
            throw new RuntimeException("listData is null,please check your params !");
        }
        if (layoutHelper == null) {
            throw new RuntimeException("layoutHelper is null,please check your params !");
        }
        if (itemLayout == 0) {
            throw new RuntimeException("itemLayout is null,please check your params !");
        }
        this.mContext = context;
        this.mResLayout = itemLayout;
        this.mLayoutHelper = layoutHelper;
        this.mDatas = listData;
    }

    public List<T> getData(){
        return mDatas;
    }

    /**
     * <br/> 方法名称: addItem
     * <br/> 方法详述: 设置单个数据源
     * <br/> 参数: mItem，单个数据源
     * <br/> 返回值:  VBaseAdapter
     */
    public void addItem(T mItem) {
        this.mDatas.add(mItem);
    }

    /**
     * <br/> 方法名称: addItems
     * <br/> 方法详述: 设置多个数据源
     * <br/> 返回值:  VBaseAdapter
     */
    public void addItems(List<T> mItem) {
        this.mDatas.addAll(mItem);
    }

    /**
     * <br/> 方法名称: removeItem
     * <br/> 方法详述: 删除单个数据源
     * <br/> 返回值:  VBaseAdapter
     */
    public void removeItem(int position) {
        this.mDatas.remove(position);
    }

    /**
     * <br/> 方法名称: removeItems
     * <br/> 方法详述: 删除多个数据源
     * <br/> 返回值:  VBaseAdapter
     */
    public void removeItems(List<T> mItem) {
        this.mDatas.removeAll(mItem);
    }

    /**
     * <br/> 方法名称: clearData
     * <br/> 方法详述: 清空数据源
     * <br/> 返回值:  VBaseAdapter
     */
    public void clearData() {
        this.mDatas.clear();
    }

    /**
     * <br/> 方法名称: setListener
     * <br/> 方法详述: 传入监听，方便回调
     * <br/> 参数: listener
     * <br/> 返回值:  VBaseAdapter
     */
    public void setItemClickListener(OnItemClickListener<T> listener) {
        this.mCLickListener = listener;
    }

    /**
     * <br/> 方法名称: setListener
     * <br/> 方法详述: 传入监听，方便回调
     * <br/> 参数: listener
     * <br/> 返回值:  VBaseAdapter
     */
    public void setItemLongClickListener(OnItemLongClickListener<T> listener) {
        this.mLongListener = listener;
    }

    /**
     * <br/> 方法名称: onCreateLayoutHelper
     * <br/> 方法详述: 继承elegateAdapter.Adapter后重写方法，告知elegateAdapter.Adapter使用何种布局管理器
     * <br/> 参数:
     * <br/> 返回值:  VBaseAdapter
     */
    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }


    /**
     * <br/> 方法名称: onCreateViewHolder
     * <br/> 方法详述: 解析布局文件，返回传入holder的构造器
     */
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return ViewHolder.createViewHolder(mContext, parent, mResLayout);
    }

    /**
     * <br/> 方法名称: onBindViewHolder
     * <br/> 方法详述: 绑定数据
     * <br/> 参数:
     * <br/> 返回值:  VBaseAdapter
     */

    @Override
    public void onBindViewHolder(ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        if(mCLickListener != null){
            holder.getConvertView().setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View view) {
                    mCLickListener.onItemClick(view, position, mDatas.get(position));
                }
            });
        }
        if(mLongListener != null){
            holder.getConvertView().setOnLongClickListener(view -> {
                if(NoDoubleClickUtils.isDoubleClick()) return true;
                mLongListener.onItemLongClick(view, position, mDatas.get(position));
                return true;
            });
        }
        convert(holder, position, mDatas.get(position));
    }

    protected abstract void convert(ViewHolder holder, int position, T item);

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public interface OnItemClickListener<T>{
        void onItemClick(View view, int position, T item);
    }

    public interface OnItemLongClickListener<T>{
        void onItemLongClick(View view, int position, T item);
    }
}
